<template>
  <div class="industryBody">
    <el-table
      :data="tableData"
      :row-class-name="rankingRowClassName"
      height="300"
      style="width:100%;"
    >
      <el-table-column type="index" :label="$t('assessment.SN')" width="60">
      </el-table-column>
      <!-- 国内技术相似 -->
      <template v-if="dataType === 'cnCompanySort'">
        <el-table-column
          prop="companyName"
          :label="$t('assessment.CompanyName')"
        >
        </el-table-column>
        <el-table-column
          prop="similarityNum"
          sortable
          :label="$t('assessment.Similarity')"
          width="89"
        >
        </el-table-column>
        <el-table-column
          prop="companyPatentNum2"
          sortable
          :label="$t('assessment.Score')"
          width="68"
        >
        </el-table-column>
      </template>
      <!-- 国外技术相似 -->
      <template v-else>
        <el-table-column
          prop="companyName"
          :label="$t('assessment.CompanyName')"
        >
        </el-table-column>
        <el-table-column
          prop="companyPatentNum2"
          sortable
          :label="$t('assessment.Score')"
          width="100"
          align="center"
        >
        </el-table-column>
      </template>

      <el-table-column
        :label="$t('assessment.Operation')"
        align="center"
        width="77"
      >
        <template slot-scope="scope">
          <v-btn class="vbtn" @click="goTechComparison(scope)">{{
            $t("assessment.Comparison")
          }}</v-btn>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  props: ["dataType", "allData"],
  data: () => ({
    tableData: [],
    getData: [false, false],
    cnData: [], // 计算最大值
    enData: [],
    max: "",
  }),
  methods: {
    getMax() {
      let arrRank = [...this.cnData, ...this.enData];
      let arrRanks = [];
      arrRank.forEach((v) => {
        arrRanks.push(v.companyPatentNum);
      });
      this.max = Math.max.apply(null, arrRanks);
      this.renderTable();
    },
    renderTable() {
      if (this.dataType === "cnCompanySort") {
        let arrRank = this.cnData;
        arrRank.forEach((v) => {
          // 分数
          if (v.companyScore) {
            v.companyPatentNum2 = v.companyScore;
          } else {
            v.companyPatentNum2 =
              Math.floor((v.companyPatentNum / this.max) * 100) / 100;
          }
          // 相似度
          let arr = v.similarity.toString().split(".");
          if (arr[1] && arr[1].length > 3) {
            v.similarityNum = Math.floor(v.similarity * 100) / 100; // v.similarity.toFixed(3)
          } else {
            v.similarityNum = v.similarity;
          }
        });
        // 获取排名（按分数升序排列）
        let b = arrRank.sort((a, b) => {
          return Number(b.companyPatentNum2) - Number(a.companyPatentNum2);
        });
        this.tableData = b;
      } else if (this.dataType === "enCompanySort") {
        let arrRank = this.enData;
        arrRank.forEach((v) => {
          // 分数
          if (v.companyScore) {
            v.companyPatentNum2 = v.companyScore;
          } else {
            v.companyPatentNum2 =
              Math.floor((v.companyPatentNum / this.max) * 100) / 100;
          }
        });
        // 获取排名（按分数升序排列）
        let b = arrRank.sort((a, b) => {
          return Number(b.companyPatentNum2) - Number(a.companyPatentNum2);
        });
        this.tableData = b;
      }
    },
    rankingRowClassName({ row, rowIndex }) {
      if (row.companyName === this.$cookie.get("companyName")) {
        return "highlight";
      }
    },
    // 跳转到技术对比
    goTechComparison(scope) {
      this.$router.push({
        name: "technicalComparison",
        query: {
          compareName: scope.row.companyName,
        },
      });
    },
  },
  watch: {
    getData() {
      if (this.getData.indexOf(false) < 0) {
        this.getMax();
      }
    },
  },
  mounted() {
    this.$Bus.$on("cnList", (data) => {
      this.cnData = data.cnCompanySort;
      this.$set(this.getData, 0, true);
    });
    this.$Bus.$on("enList", (data) => {
      this.enData = data.enCompanySort;
      this.$set(this.getData, 1, true);
    });
  },
};
</script>

<style lang="scss" scoped>
.vbtn {
  width: 85%;
  min-width: 85%;
  height: 30px;
  border-radius: 4px;
  font-size: 12px;
  color: $mainColor;
  background-color: $hoverColor !important;
}
.vbtn:hover {
  font-weight: 600;
  color: #fff;
  background-color: $mainColor !important;
}
</style>
